package hc.bre.deci

import static org.junit.jupiter.api.Assertions.*

import hc.bre.deci.tree.TreeBuilder
import hc.bre.deci.tree.TreeDefinition

import java.lang.reflect.*
import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest

@SpringBootTest
@groovy.transform.CompileStatic
class TreeTest {

	@Test
	void create() {
		TreeDefinition ruleDef;
		ruleDef = new TreeDefinition();
		ruleDef = TreeDefinition.create();
	}


	@Test
	void createByStaticMethodReflection() {
		Method create= hc.Util.getStaticMethod(TreeDefinition.class,'create');
		TreeDefinition ruleDef = (hc.bre.deci.tree.TreeDefinition) create.invoke(TreeDefinition.class);
	}

	String jsonText='''
{
  "rid" : 308,
  "rtype" : "rule:deci:tree",
  "rname" : "决策树2",
  "rpath" : "xsxdjc/JueCeShu2",
  "rversion" : 1,
  "rmemo" : "",
  "rtest" : "{}",
  "rmaxid" : 1,
  "actionmaxid" : 1,
  "actionDefList" : [ {
    "id" : 1,
    "code" : "行动1",
    "name" : "行动1",
    "type" : "ValueSetter",
    "refer" : "text",
    "resultMap" : { }
  } ],
  "root" : {
    "id" : 1,
    "referId" : 0,
    "referName" : "开始",
    "list" : [ ],
    "rid" : 0,
    "rname" : "",
    "valueMap" : {
      "行动1" : "用户输入简单内容"
    }
  }
}
'''
	@Test
	void load() {
		TreeDefinition ruleDef;

		ruleDef = new TreeDefinition().load(hc.Util.jsonRead(jsonText));
		assert ruleDef.actionDefList[0].name =='行动1'
		assert ruleDef.root.valueMap['行动1'] =='用户输入简单内容'
	}

	@Test
	void build() {
		TreeDefinition ruleDef;
		ruleDef = new TreeDefinition().load(jsonText) as TreeDefinition;
		String script = new TreeBuilder().forRule(ruleDef).build()
		assert script.indexOf("rule") > 0
//		println script;
	}

	@Test
	void compile() {
		TreeDefinition ruleDef = new TreeDefinition();
		ruleDef.load(hc.Util.jsonRead(jsonText));
		
		ruleDef.buildRuleClass();
	}
	
	@Test
	void evaluate() {
		TreeDefinition ruleDef = new TreeDefinition();
		ruleDef.load(hc.Util.jsonRead(jsonText));
		def ret = ruleDef.buildRuleEvaluate([:]);
		assert ret['result']['branchId'] == 1
//		println ret;
	}
}
